/**
 *	bbx 
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');

	//页面跳转管理类
	var WindowTools = require('WindowTools_Core');
	var baseService = require('Base_Service');
	var systemService = require('System_Service');
	var trainService = require('Train_Service');

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	var user;
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			mui.init({
				statusBarBackground: '#f7f7f7'
			});
			user = baseService.getUser();
			debugger;
			WindowTools.preloadTemplate();
			UIInit();
			clickInit();
		});
	}
	/**
	 * ui的初始化
	 */
	function UIInit() {
		systemService.getDrivingTypes(function(data) {
			//			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.Value);
				option.innerHTML = item.Text;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('driveType');
			dataContainer.appendChild(fragment);
		});
		
		systemService.getPlaces(user.DrivingID,function(data){
//			debugger;
			var fragment = document.createDocumentFragment();
			for(var i = 0; i < data.length; i++) {
				var item = data[i];
				var option;
				option = document.createElement('option');
				option.setAttribute("value", item.id);
				option.innerHTML = item.name;
				fragment.appendChild(option);
			}
			var dataContainer = document.getElementById('place');
			dataContainer.appendChild(fragment);
		});

	}

	/**
	 * 点击事件初始化
	 */
	function clickInit() {
		var commit = document.getElementById('commit'); //提交按钮
		var history = document.getElementById('history'); //提交按钮

		history.addEventListener("tap",function(){
			WindowTools.openWinWithTemplate("","train.html");
		});

		commit.addEventListener("tap", function() {

			//学时类型选择
			var studyTime = document.getElementById('studyTime');
			var indexSex1 = studyTime.selectedIndex;
			var studyTimeValue = studyTime.options[indexSex1].value;

			//科目类型选择
			var course = document.getElementById('course');
			var indexSex2 = course.selectedIndex;
			var courseValue = course.options[indexSex2].value;

			//时间选择
			var time = document.getElementById('time');
			var indexSex3 = time.selectedIndex;
			var timeValue = time.options[indexSex3].value;

			//驾照类型选择
			var driveType = document.getElementById('driveType');
			var driveIndex = driveType.selectedIndex;
			var driveValue = driveType.options[driveIndex].value;

			var data = {
				"PlaceID": 1,
				"TimeSoltType": studyTimeValue,
				"CourseType": courseValue,
				"TotalHours": timeValue,
				"DrivingLicenseType": driveValue,
				"DrivingID": 1

			};
			
			trainService.add(data, function(data) {
				debugger;
			});
		})
	}
});